package com.net.framework.help.manager;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangxiaobin
 * @version 1.1.0
 * @email zhangxiaobin@yipintech.com
 * @date 创建时间：2015年6月23日 下午5:09:11
 * @explain activity栈管理
 */
public class ActivityStackManager {
    // activity数组
    private List<Activity> listActivity;
    private static ActivityStackManager activityStackManager;

    public ActivityStackManager() {
        // TODO Auto-generated constructor stub
    }

    public static ActivityStackManager getInstance() {
        if (null == activityStackManager) {
            activityStackManager = new ActivityStackManager();
        }
        return activityStackManager;
    }

    public void addActivity(Activity activity) {
        if (listActivity == null) {
            listActivity = new ArrayList<Activity>();
        }
        listActivity.add(activity);
    }

    public void finishAll() {
        if (listActivity != null)
            for (Activity activity : listActivity) {
                if (!activity.isFinishing())
                    activity.finish();
            }
        listActivity.clear();
        listActivity = null;
    }

    /***
     * 是否没有存储本地activity
     *
     * @return
     */
    public boolean isHasActivityList() {
        if (getListSize() == 1 || getListSize() == 0) {
            return false;
        }
        return true;
    }

    /**
     * 获取集合大小
     */
    public int getListSize() {
        if (listActivity == null) {
            return 0;
        }
        return listActivity.size();
    }

    /**
     * 移除指定activity
     */
    public void removeActivity(Activity activity) {
        if (listActivity != null) {
            listActivity.remove(activity);
        }
    }

    /**
     * 判断是否有此activity
     */
    public boolean isHasActivity(Activity activity) {
        if (listActivity != null) {
            return listActivity.contains(activity);
        }
        return false;
    }

    /**
     * 判断是否有此activity
     */
    public boolean isHasActivity(Class<?> cls) {
        for (int i = 0; i < listActivity.size(); i++) {
            Activity activity = listActivity.get(i);
            if (activity.getClass().equals(cls)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 结束指定类名的Activity
     */
    public void killActivity(Class<?> cls) {
        for (int i = 0; i < listActivity.size(); i++) {
            Activity activity = listActivity.get(i);
            if (activity.getClass().equals(cls)) {
                killActivity(activity);
            }
        }
    }

    public Activity getActivity(Class<?> cls) {
        for (int i = 0; i < listActivity.size(); i++) {
            Activity activity = listActivity.get(i);
            if (activity.getClass().equals(cls)) {
                return activity;
            }
        }
        return null;
    }

    /**
     * 结束指定的同类activity
     */
    public void finishActivityByName(Activity activity) {
        String className = activity.getClass().getName();
        List<Activity> removeAcList = new ArrayList<Activity>();
        if (listActivity != null) {
            for (Activity ac : listActivity) {
                if (className.equals(ac.getClass().getName())) {
                    ac.finish();
                    removeAcList.add(ac);
                }
            }
            listActivity.removeAll(removeAcList);
        }
    }

    /**
     * 结束指定的Activity
     */
    public void killActivity(Activity activity) {
        if (activity != null) {
            listActivity.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    public void removeListActivity(Activity activity) {
        if (activity != null) {
            listActivity.remove(activity);
        }
    }

    /***
     * 获取栈顶Activity（堆栈中最后一个压入的）
     *
     * @return Activity
     */
    public void getKillTopActivity() {
        if (listActivity == null) {
            return;
        }
        Activity activity = listActivity.get(getListSize() - 1);
        killActivity(activity);
    }
}
